#include "parametermodel.h"

ParameterModel::ParameterModel(QObject *parent) : QAbstractListModel(parent) {}

ParameterModel::~ParameterModel() { clear(); }

int ParameterModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_itemList.count();
}

QVariant ParameterModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_itemList.count())
        return QVariant();

    ParameterItem *item = m_itemList[index.row()];
    switch (role) {
    case Qt::UserRole + 1: return item->id();
    case Qt::UserRole + 2: return item->Name();
    case Qt::UserRole + 3: return item->Process1();
    case Qt::UserRole + 4: return item->Process2();
    case Qt::UserRole + 5: return item->F_UpperKnife01Assist();
    case Qt::UserRole + 6: return item->F_UpperKnife02Assist();
    case Qt::UserRole + 7: return item->B_UpperKnife01Assist();
    case Qt::UserRole + 8: return item->B_UpperKnife02Assist();
    case Qt::UserRole + 9: return item->F_UpperKnife01();
    case Qt::UserRole + 10: return item->F_UpperKnife02();
    case Qt::UserRole + 11: return item->B_UpperKnife01();
    case Qt::UserRole + 12: return item->B_UpperKnife02();
    case Qt::UserRole + 13: return item->F_LowerKnife01();
    case Qt::UserRole + 14: return item->F_LowerKnife02();
    case Qt::UserRole + 15: return item->B_LowerKnife01();
    case Qt::UserRole + 16: return item->B_LowerKnife02();
    case Qt::UserRole + 17: return item->Roller();
    default: return QVariant();
    }
}

QHash<int, QByteArray> ParameterModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles.insert(Qt::UserRole + 1, "id");
    roles.insert(Qt::UserRole + 2, "Name");
    roles.insert(Qt::UserRole + 3, "Process1");
    roles.insert(Qt::UserRole + 4, "Process2");
    roles.insert(Qt::UserRole + 5, "F_UpperKnife01Assist");
    roles.insert(Qt::UserRole + 6, "F_UpperKnife02Assist");
    roles.insert(Qt::UserRole + 7, "B_UpperKnife01Assist");
    roles.insert(Qt::UserRole + 8, "B_UpperKnife02Assist");
    roles.insert(Qt::UserRole + 9, "F_UpperKnife01");
    roles.insert(Qt::UserRole + 10, "F_UpperKnife02");
    roles.insert(Qt::UserRole + 11, "B_UpperKnife01");
    roles.insert(Qt::UserRole + 12, "B_UpperKnife02");
    roles.insert(Qt::UserRole + 13, "F_LowerKnife01");
    roles.insert(Qt::UserRole + 14, "F_LowerKnife02");
    roles.insert(Qt::UserRole + 15, "B_LowerKnife01");
    roles.insert(Qt::UserRole + 16, "B_LowerKnife02");
    roles.insert(Qt::UserRole + 17, "Roller");
    return roles;
}

void ParameterModel::addItem(ParameterItem *item)
{
    if (!item) return;
    beginInsertRows(QModelIndex(), m_itemList.count(), m_itemList.count());
    m_itemList.append(item);
    endInsertRows();
    emit modelDataUpdated();
}

void ParameterModel::removeByIndex(int index)
{
    if (index < 0 || index >= m_itemList.count()) return;
    beginRemoveRows(QModelIndex(), index, index);
    delete m_itemList.takeAt(index);
    endRemoveRows();
    emit modelDataUpdated();
}

ParameterItem *ParameterModel::itemAt(int index)
{
    if (index < 0 || index >= m_itemList.count()) return nullptr;
    return m_itemList[index];
}

void ParameterModel::clear()
{
    beginResetModel();
    qDeleteAll(m_itemList);
    m_itemList.clear();
    endResetModel();
    emit modelDataUpdated();
}

void ParameterModel::copyModel(ParameterModel *model)
{
    if (!model) return;
    clear();
    for (int i = 0; i < model->count(); ++i) {
        ParameterItem *src = model->itemAt(i);
        ParameterItem *dest = new ParameterItem();
        dest->copyItem(src);
        addItem(dest);
    }
}

void ParameterModel::freshData()
{
    beginResetModel();
    endResetModel();
    emit modelDataUpdated();
}
